/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package actionsUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.beans.MessageBroadsBean;
import models.beans.User;
import models.dao.MessageBroadDAO;
import models.dao.UserDAO;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
 *
 * @author Group 3 Aprotrain Aptech  Group3 Aprotrain Aptech
 */
public class loginUser extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            User UserLogin = (User) form;
            User user = UserDAO.loginUser(UserLogin.getUserId(), UserLogin.getPassword());
            //System.out.println(user);
            if(user == null){
                request.setAttribute("errorLoginUser", "UserName and Password are not correct.");
                return mapping.findForward("loginFalse");
            }
            else{
                User us = (User) form;
                us.setUserName(user.getUserName());
                us.setUserId(user.getUserId());
                request.getSession().setAttribute("user", us);
                UserDAO.updateStatus(1,user.getUserId());
                if(user.getIsStaff() == 0){
                    request.getSession().setAttribute("isUser", "okStudent");
                }
                else if(user.getIsStaff() == 1){
                    request.getSession().setAttribute("isStaff", "okStaff");
                }             

                if(user.getIsFirstLogin() == 0 && user.getIsStaff() == 0){
                    request.getSession().setAttribute("changePassStudent", "okStudent");                   
                    return mapping.findForward("changePassStudent");
                }
                else if(user.getIsFirstLogin() == 0 && user.getIsStaff() == 1){
                    request.getSession().setAttribute("changePassStaff", "okStaff");                  
                    return mapping.findForward("changePassStaff");
                }
                else{
                    return mapping.findForward("loginUserOk");
                }
            }
    }
}
